package com.k2data.k2app.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.k2data.k2app.common.MyConstant;
import com.k2data.k2app.common.WaringDealType;
import com.k2data.k2app.domain.deviceRest.Event;
import com.k2data.k2app.domain.deviceRest.REventPoint;
import com.k2data.k2app.domain.po.EarlyWaringDO;
import com.k2data.k2app.domain.po.JobTaskDO;
import com.k2data.k2app.domain.vo.Job;
import com.k2data.k2app.domain.vo.Model;
import com.k2data.k2app.exception.K2ResponseException;
import com.k2data.k2app.mapper.EarlyWaringMapper;
import com.k2data.k2app.rest.MsgClient;
import com.k2data.k2app.service.pas04.Pas04ResultService;
import com.k2data.k2app.util.DateFormatterUtil;
import com.k2data.k2app.util.Encryption3DES;
import com.k2data.k2app.util.JsonUtils;
import com.k2data.k2app.utils.StringUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预警处理
 * Created by clb on 17-4-18.
 */
@Log4j2
@Service
public class EarlyWarningService {

    @Autowired
    private EarlyWaringMapper earlyWaringMapper;
    @Autowired
    private Pas04ResultService pas04ResultService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private JobService jobService;
    @Autowired
    MsgClient msgClient;
    @Autowired
    SystemService systemService;
    @Autowired
    private ModelService modelService;
    /**
     * 接收并处理告警
     * @param jobTaskDO
     */
    @Transactional
    public void dealWaring(JobTaskDO jobTaskDO) throws Exception {
        log.debug("开始处理预警---start deal warning---");
        Long projectId = jobTaskDO.getProjectId();
        Long jobId = jobTaskDO.getJobId();
        Long modelId = jobTaskDO.getModelId();
        //查询pas分析结果
        List result=  pas04ResultService.getInterfaces(projectId, jobId, null, null, null);
        //没有数据，直接返回
        if(result.size()==0) {return;}
        List<EarlyWaringDO> list = Lists.newArrayList();
        for(int i =0;i<result.size();i++){
            EarlyWaringDO earlyWaringDO = jsonToWaring(result.get(i).toString(), jobId);
            earlyWaringDO.setJobId(jobId);
            earlyWaringDO.setModelId(modelId);
            list.add(earlyWaringDO);
        }
        //获取作业的处理方式，进行不同的处理
        String[] actions = jobTaskDO.getTaskDO().getAction().split(",");
        if(actions.length == 0){
            //预警不发送任何消息,留存记录
            list.stream().forEach(item->{item.setDealType(WaringDealType.DEAL_NO.getValue());});
            insertBatch(list);
        }else{
            //过滤时间段内出现的重复告警
            List<EarlyWaringDO> earlyWaringDOs = fiterWaring(MyConstant.FILTER_TIME,list);
            //设备信息
            for (EarlyWaringDO earlyWaringDO : earlyWaringDOs) {
                earlyWaringDO.setDevice(deviceService.findDeviceByCode(earlyWaringDO.getDeviceNo()));
            }
            //按照用户分组,方便给每个用户一次性发送其负责的设备故障信息，而不是每个故障消息发一条邮件
            Map<Long,List<EarlyWaringDO>> map = groupOwner(earlyWaringDOs);
            //告警信息
            for(String str:actions){
                WaringDealType waringDealType = WaringDealType.getTypeByName(str);
                switch (waringDealType){
                    case DEAL_SMS:
                        sendSms(map);
                        earlyWaringDOs.stream().forEach(item->{
                            item.setDealType(WaringDealType.DEAL_SMS.getValue());
                            item.setIsSms(MyConstant.YES_INTEGER);
                        });
                        break;
                    case DEAL_MAIL:
                        sendMail(map);
                        earlyWaringDOs.stream().forEach(item->{
                            item.setDealType(WaringDealType.DEAL_MAIL.getValue());
                            item.setIsMail(MyConstant.YES_INTEGER);
                        });
                        break;
                    default:
                        earlyWaringDOs.stream().filter(earlyWaringDO -> earlyWaringDO.getDealType()==null).forEach(item->{
                            item.setDealType(WaringDealType.DEAL_AMQ.getValue());
                            item.setIsMq(MyConstant.YES_INTEGER);
                        });
                }
            }
            //处理的预警
            insertBatch(earlyWaringDOs);
            //不做处理的重复数据
            list.removeAll(earlyWaringDOs);
            list.stream().forEach(item->{item.setDealType(WaringDealType.DEAL_NEVER.getValue());});
            insertBatch(list);
        }
    }

    @Transactional
    public Integer insert(EarlyWaringDO earlyWaringDO){
        int	result = earlyWaringMapper.insert(earlyWaringDO);
        return result;
    }


    @Transactional
    public List<EarlyWaringDO> count(){
        List<EarlyWaringDO> count = earlyWaringMapper.count();
        return count;
    }

    @Transactional
    public Integer insertBatch(List<EarlyWaringDO> list){
        if(list.size() == 0) {return 1;}
        int	result = earlyWaringMapper.insertBatch(list);
        return result;
    }

    public List<EarlyWaringDO> list(EarlyWaringDO earlyWaringDO) throws Exception {
        Map<String,Map> earlyWaringDevice = new HashMap<>();
        List<EarlyWaringDO> list = earlyWaringMapper.selectWarnings(earlyWaringDO);
        if(list != null){
            for(int i = 0; i< list.size(); i++){
                EarlyWaringDO earlyWaringDO1 = list.get(i);
                if(earlyWaringDO1.getDeviceNo() != null){
                    Map device = earlyWaringDevice.get(earlyWaringDO1.getDeviceNo());
                    if(device == null){
                        device = deviceService.findDeviceByCode(earlyWaringDO1.getDeviceNo());
                        if(device != null && device.size()>0) {
                            earlyWaringDevice.put(earlyWaringDO1.getDeviceNo(),device);
                        }
                    }
                    earlyWaringDO1.setDevice(device);
                }
            }
        }
        return list;
    }

    public List<EarlyWaringDO> listWithModel(EarlyWaringDO earlyWaringDO) throws Exception {
        Map<String,Map> earlyWaringDevice = new HashMap<>();
        Map<Long,Model> earlyWaringModel = new HashMap<>();
        List<EarlyWaringDO> list = earlyWaringMapper.selectWarnings(earlyWaringDO);
        if(list != null){
            for(int i = 0; i< list.size(); i++){
                EarlyWaringDO earlyWaringDO1 = list.get(i);
                if(earlyWaringDO1.getDeviceNo()!=null){
                    Map device = earlyWaringDevice.get(earlyWaringDO1.getDeviceNo());
                    if(device == null){
                        device = deviceService.findDeviceByCode(earlyWaringDO1.getDeviceNo());
                        if(device != null && device.size()>0) {
                            earlyWaringDevice.put(earlyWaringDO1.getDeviceNo(),device);
                        }
                    }
                    earlyWaringDO1.setDevice(device);
                }
                if(earlyWaringDO1.getModelId() != null){
                    try {
                        Model model = earlyWaringModel.get(earlyWaringDO1.getModelId());
                        if(model == null){
                            model = modelService.get(earlyWaringDO1.getModelId(),false);
                            earlyWaringModel.put(earlyWaringDO1.getModelId(),model);
                        }
                        earlyWaringDO1.setModel(model);
                    } catch (Exception e) {
                        log.debug("获取作业时出错,jobid:"+earlyWaringDO1.getJobId());
                        continue;
                    }
                }
            }
        }
        return list;
    }

    public EarlyWaringDO getWithModel(Long id) throws Exception {
        EarlyWaringDO earlyWaringDO1 = earlyWaringMapper.getById(id);
        Map<Long,Model> earlyWaringModel = new HashMap<>();
        if(earlyWaringDO1.getModelId() != null){
            try {
                Model model = earlyWaringModel.get(earlyWaringDO1.getModelId());
                if(model == null){
                    model = modelService.get(earlyWaringDO1.getModelId(),false);
                    earlyWaringModel.put(earlyWaringDO1.getModelId(),model);
                }
                earlyWaringDO1.setModel(model);
            } catch (Exception e) {
                log.debug("获取作业时出错,jobid:"+earlyWaringDO1.getJobId());
            }
        }
        return earlyWaringDO1;
    }

    /**
     * 处理告警;
     * 忽略;生成工单
     */
    @Transactional
    public int dealStatusV1(EarlyWaringDO earlyWaringDO1) throws Exception{
        int dealType = 0;
        if(WaringDealType.DEAL_ORDER.getName().equals(earlyWaringDO1.getDealTypeName())){
            //告警生成工单
            EarlyWaringDO earlyWaringDO = getWithModel(earlyWaringDO1.getId());
            Map deviceMap = deviceService.findDeviceByCode(earlyWaringDO.getDeviceNo());
            String deviceId = deviceMap.get("id").toString();
            String customerId = String.valueOf(deviceMap.getOrDefault("customerId",""));
            Event event = new Event();
            event.setDeviceId(Long.valueOf(deviceId));
            event.setCustomerId(Long.valueOf(customerId));
            //部件
            Job job = jobService.get(earlyWaringDO.getJobId());
            if(job.getTask()!=null && !StringUtils.isEmpty(job.getTask().getDeviceParts())){
                Map templateDeviceById = deviceService.getTemplateDeviceById(Long.valueOf(job.getTask().getDeviceParts()));
                event.setComponents(String.valueOf(templateDeviceById.getOrDefault("name","")));
            }
            Model model = earlyWaringDO.getModel();
            if(model!=null){
                event.setEventName(model.getName());
                event.setEventType(model.getName());
            }else{
                event.setEventName("实时预警工单");
//				event.setEventType(model.getName());
            }
            event.setEventDesc(earlyWaringDO.getMessage());
//            event.setType(earlyWaringDO.getType());
            event.setType("maintenance");
            event.setEventStatus("未解决");
            event.setEventDesc(earlyWaringDO.getMessage());
            if(job.getTask()!=null){
                if(job.getTask().getKmxVariableName()!=null){
                    List<REventPoint> eventPoints = Lists.newArrayList();
                    String[] points = job.getTask().getKmxVariableName().split("#")[0].split("_");
                    Map point = deviceService.getDevicePoint(Long.valueOf(points[2]),Long.valueOf(points[0]),Integer.valueOf(points[1]));

                    //name_en : c_vib
                    String nameEn = point.get("nameEn").toString();
                    //sensorName: 振动量
                    String sensorName = point.get("sensorName").toString();
                    //name : 风排111Y
                    String channelName = point.get("name").toString();


                    String[] varibles = job.getTask().getKmxVariableName().split("#")[1].split(",");

                    for(String s:varibles){
                        Map varible = deviceService.getDeviceVariablesById(Long.valueOf(s));
                        REventPoint rEventPoint = new REventPoint();
                        rEventPoint.setChannelId(points[1]);
                        rEventPoint.setVariableNameEn(varible.get("variableNameEn").toString());
                        rEventPoint.setCheckName(varible.get("variableName").toString()+"_"+channelName);
                        rEventPoint.setSensorName(sensorName);
                        rEventPoint.setSensorNameEn(nameEn);
                        eventPoints.add(rEventPoint);
                    }
                    event.setEventPonts(eventPoints);
                }
            }

            deviceService.addEvent(event);
            dealType = WaringDealType.DEAL_ORDER.getValue();
            earlyWaringDO1.setIsMq(3);
        }else{
            //告警忽略
            dealType = WaringDealType.DEAL_IGNORE.getValue();
            earlyWaringDO1.setIsMq(2);
        }
        earlyWaringDO1.setDealType(dealType);
        int i = earlyWaringMapper.updateByPrimaryKeySelective(earlyWaringDO1);
        return i;
    }


    /**
     * json字符串转为告警对象
     * @param jsonStr
     * @return
     */
    public EarlyWaringDO jsonToWaring(String jsonStr, Long jobId) throws Exception{
        Map<String, Object> job = JsonUtils.convertJsonStrToMap(jsonStr);
        Job jobObject = jobService.get(jobId);
        String deviceno = job.get("deviceno")==null?"":job.get("deviceno").toString();
        String message = job.get("message")==null?"":job.get("message").toString();
        String level = job.get("level")==null?"":job.get("level").toString();
        String type = job.get("warningtype")==null?"":job.get("warningtype").toString();
        String ts = job.get("ts").toString().replace("T"," ").replace("Z","");
        String faultName = jobObject.getProjNameTemplate();
        LocalDateTime localDateTime1 = DateFormatterUtil.convertToType(LocalDateTime.class,ts);
        LocalDateTime localDateTime = localDateTime1 == null?LocalDateTime.now():localDateTime1;
        EarlyWaringDO earlyWaringDO = new EarlyWaringDO();
        earlyWaringDO.setDeviceNo(deviceno);
        earlyWaringDO.setMessage(message);
        earlyWaringDO.setLevel(level);
        earlyWaringDO.setType(type);
        earlyWaringDO.setWaringTime(localDateTime);
        earlyWaringDO.setFaultName(faultName);
        return earlyWaringDO;
    }

    /**
     * 过滤掉时间段内存在的重复告警
     *
     * @param list
     * @return
     */
    public List<EarlyWaringDO> fiterWaring(Long timePart, List<EarlyWaringDO> list){
        List<EarlyWaringDO> earlyWaringDOs =  Lists.newArrayList();
        list.stream().forEach(item -> {
            if(!isHaveSameBetweenTime(timePart,item)){
                //告警的设备信息
                earlyWaringDOs.add(item);
            }
        });
        return earlyWaringDOs;
    }

    /**
     * 判断时间范围内是否已经有过一样的告警
     *
     * @param timePart
     * @param earlyWaringDO
     */
    public boolean isHaveSameBetweenTime(Long timePart,EarlyWaringDO earlyWaringDO){
        LocalDateTime endtime = LocalDateTime.now();
        LocalDateTime starttime = endtime.minusMinutes(timePart);
        earlyWaringDO.setStartTime(starttime);
        earlyWaringDO.setEndTime(endtime);
        List<EarlyWaringDO> earlyWaringDOs = earlyWaringMapper.selectWarnings(earlyWaringDO);
        if(earlyWaringDOs.size()>0) {return true;}
        return false;
    }

    public void sendSms(Map<Long,List<EarlyWaringDO>> map) throws Exception {
        if(map != null && map.size()>0) {
            //循环客户,发送消息
            for (Map.Entry<Long, List<EarlyWaringDO>> entry : map.entrySet()) {
                // 获取用户信息
                Map param = getSmsInfoByUser(entry.getKey());
                if(param != null && param.size()>0){
                    log.debug(String.format("给用户<%s><%s>发送短信！",param.get("userName").toString(),param.get("phoneNumber").toString()));
                    try {
                        msgClient.sendSingleSms(JsonUtils.mapToJson(param));
                        systemService.insertLog(this.getClass().getName(),
                                Thread.currentThread().getStackTrace()[1].getMethodName(),
                                "发短信",
                                String.format("给用户<%s><%s>发送短信", param.get("userName").toString(), param.get("phoneNumber").toString()),
                                SystemService.LOG_NORMAL);
                    } catch (Exception e) {
                        systemService.insertLog(this.getClass().getName(),
                                Thread.currentThread().getStackTrace()[1].getMethodName(),
                                "发送短信失败",
                                String.format("给用户<%s><%s>发送短信失败!%s",param.get("userName").toString(),param.get("phoneNumber").toString(),e.getMessage()),
                                SystemService.LOG_ERROR);
                    }
                }
            }
        }
    }

    public void sendMail(Map<Long,List<EarlyWaringDO>> map) throws Exception {
        if(map != null && map.size()>0) {
            //循环客户,发送消息
            for(Map.Entry<Long,List<EarlyWaringDO>> entry: map.entrySet()){
                // 获取用户信息
                Map param = getSmaiInfoByUser(entry.getKey(), entry.getValue());
                if(param != null && param.size()>0){
                    log.debug(String.format("给用户<%s><%s>发送邮件！",param.get("toUserName").toString(),param.get("sendTO").toString()));
                    try {
                        msgClient.sendMail(JsonUtils.mapToJson(param));
                        systemService.insertLog(this.getClass().getName(),
                                Thread.currentThread().getStackTrace()[1].getMethodName(),
                                "发邮件",
                                String.format("给用户<%s><%s>发送邮件",param.get("toUserName").toString(),param.get("sendTO").toString()),
                                SystemService.LOG_NORMAL);
                    } catch (Exception e) {
                        systemService.insertLog(this.getClass().getName(),
                                Thread.currentThread().getStackTrace()[1].getMethodName(),
                                "发邮件失败",
                                String.format("给用户<%s><%s>发送邮件失败!%s",param.get("toUserName").toString(),param.get("sendTO").toString(),e.getMessage()),
                                SystemService.LOG_ERROR);
                    }
                }

            }
        }
    }

    /**
     * 发送邮件信息参数对象
     *
     * @param contactId
     * @param list
     * @return
     * @throws K2ResponseException
     */
    public Map getSmaiInfoByUser(Long contactId,List<EarlyWaringDO> list) throws Exception {
        Map mail = systemService.getUsefulMailSetting();
        if(mail == null || mail.size() == 0){
            return null;
        }
        Map user = deviceService.getContactById(contactId);
        if(user.size() == 0){
            return null;
        }
        Map map1 = new HashMap<>();
        map1.put("toUserName",user.get("name").toString());
        map1.put("message","以下设备出现预警,请查看!");
        StringBuffer sb = new StringBuffer("<table><thead><tr><th>设备</th><th>故障时间</th></tr></thead><tbody>");
        for(EarlyWaringDO messageVO2 : list){
            sb.append("<tr><td>").append(messageVO2.getDeviceNo())
                    .append("</td><td>").append(DateFormatterUtil.parseToString(messageVO2.getWaringTime()))
                    .append("</td></tr>");
        }
        sb.append("</tbody></table>");
        map1.put("content",sb.toString());
        Map<String,Object> map = new HashMap<>();
        map.put("host",mail.get("smtpAddr").toString());
        map.put("port",mail.get("smtpPort").toString());
        map.put("sendFrom",mail.get("sendEmail").toString());

        Encryption3DES encryption3DES = new Encryption3DES();
        String  s = encryption3DES.get3DESDecrypt(mail.get("emailPassword").toString(),"k2data");
        map.put("pwd",s);
        map.put("sendTO",user.get("email").toString());
        map.put("subject","预警消息");//主题
        map.put("templateName","waringTemplate.vm");
        map.put("templateParams",map1);
        return map;
    }

    /**
     * 发送短信信息参数对象
     *
     * @param contactId
     * @return
     * @throws K2ResponseException
     */
    public Map getSmsInfoByUser(Long contactId) throws Exception {
        Map user = deviceService.getContactById(contactId);
        if(user.size() == 0){
            return null;
        }
        // 短信配置
        Map mail = systemService.getUsefulSmsSetting();
        if(mail == null || mail.size() == 0){
            return null;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("phoneNumber",user.get("phoneNum").toString());
        map.put("userName",user.get("name").toString());
        map.put("signName",mail.get("smsSp").toString());
        map.put("templateCode",mail.get("smsKey").toString());
        return map;
    }

    /**
     * 告警按照用户分组，给用户发送告警
     * @param earlyWaringDOs
     * @return
     * @throws Exception
     */
    public Map<Long,List<EarlyWaringDO>> groupOwner(List<EarlyWaringDO> earlyWaringDOs) throws Exception {
        Map<Long,List<EarlyWaringDO>> map = Maps.newHashMap();

        if(earlyWaringDOs.size() == 0) {return map;}
        Map<String,Map> scores = new HashMap<>();
        for(EarlyWaringDO earlyWaringDO : earlyWaringDOs){
            //查询不到设备信息
            if(earlyWaringDO.getDevice() == null) {
                systemService.insertLog(this.getClass().getName(),
                        Thread.currentThread().getStackTrace()[1].getMethodName(),
                        "设备不存在",
                        String.format("告警信息中，设备［%s］不存在",earlyWaringDO.getDeviceNo()),
                        SystemService.LOG_WARING);
                log.debug(String.format("没有查询到设备［%s］",earlyWaringDO.getDeviceNo()));
                continue;
            }
            // 设备联系人
            List contacts = (List) earlyWaringDO.getDevice().get("contacts");
            if(contacts.size()==0){
                systemService.insertLog(this.getClass().getName(),
                        Thread.currentThread().getStackTrace()[1].getMethodName(),
                        "设备没有负责人",
                        String.format("设备没有负责人，不会发送信息[%s]",earlyWaringDO.getDeviceNo()),
                        SystemService.LOG_WARING);
                log.debug(String.format("设备没有负责人，不会发送信息[%s]",earlyWaringDO.getDeviceNo()));
                continue;
            }else{
                Long id = 0L;
                // 查询设备的主联系人
                for (int i = 0; i < contacts.size(); i++) {
                    Map contact = (Map) contacts.get(i);
                    if (contact.get("importance") != null && contact.get("importance").toString().equals("primary")) {
                        id = Long.parseLong(contact.get("id").toString());
                    }
                }
                // 设备没有主联系人，以第一个联系人为准
                if (id == 0) {
                    Map contact = (Map)contacts.get(0);
                    id = Long.parseLong(contact.get("id").toString());
                }
                List<EarlyWaringDO> list = map.get(id);
                if(list == null){
                    list = new ArrayList<>();
                }
                list.add(earlyWaringDO);
                map.put(id,list);
            }

            //设备的状态信息，以最后一个告警的状态为准
            Map device = scores.get(earlyWaringDO.getDeviceNo());
            if(device == null){
                device = new HashMap();
                device.put("id",earlyWaringDO.getDevice().get("id").toString());
            }
            device.put("status",earlyWaringDO.getLevel());
            scores.put(earlyWaringDO.getDeviceNo(),device);
        }
        //更新设备状态
        updateDeviceStatus(scores);
        return map;
    }

    /**
     * 告警按照用户分组，给用户发送告警
     * @param earlyWaringDOs
     * @return
     * @throws Exception
     */
    public Map<String,List<EarlyWaringDO>> groupOwner2(List<EarlyWaringDO> earlyWaringDOs) throws Exception {
        Map<String,List<EarlyWaringDO>> map = new HashMap<>();
        if(earlyWaringDOs.size() == 0) {return map;}
        Map<String,Map> scores = new HashMap<>();
        for(EarlyWaringDO earlyWaringDO : earlyWaringDOs){
            //查询不到设备信息
            if(earlyWaringDO.getDevice() == null) {
                systemService.insertLog(this.getClass().getName(),
                        Thread.currentThread().getStackTrace()[1].getMethodName(),
                        "设备不存在",
                        String.format("告警信息中，设备［%s］不存在",earlyWaringDO.getDeviceNo()),
                        SystemService.LOG_WARING);
                log.debug(String.format("没有查询到设备［%s］",earlyWaringDO.getDeviceNo()));
                continue;
            }
            // 设备联系人
            String loginName = "";
            List contacts = (List) earlyWaringDO.getDevice().get("contacts");
            if(contacts.size()==0){
//            if(StringUtils.isEmpty(earlyWaringDO.getDevice().get("owner").toString())){
                systemService.insertLog(this.getClass().getName(),
                        Thread.currentThread().getStackTrace()[1].getMethodName(),
                        "设备没有负责人",
                        String.format("设备没有负责人，不会发送信息[%s]",earlyWaringDO.getDeviceNo()),
                        SystemService.LOG_WARING);
                log.debug(String.format("设备没有负责人，不会发送信息[%s]",earlyWaringDO.getDeviceNo()));
                continue;
            }else{

                loginName = earlyWaringDO.getDevice().get("owner").toString();
            }
            List<EarlyWaringDO> list = map.get(loginName);
            if(list == null){
                list = new ArrayList<>();
            }
            list.add(earlyWaringDO);
            map.put(loginName,list);

            //评分信息
            Map device = scores.get(earlyWaringDO.getDeviceNo());
            if(device == null){
                device = new HashMap();
                device.put("id",earlyWaringDO.getDevice().get("id").toString());
            }
            device.put("status",earlyWaringDO.getLevel());
            scores.put(earlyWaringDO.getDeviceNo(),device);
        }
        //更新设备状态
        updateDeviceStatus(scores);
        return map;
    }

    /**
     *更新设备状态
     * @param status 更新后的设备信息 ,key： 设备编号 value： 设备信息
     * @throws Exception
     */
    public void updateDeviceStatus(Map<String,Map> status) throws Exception {
        if(status.size() == 0) {return;}
        for(Map.Entry<String,Map> item : status.entrySet()){
            Map device = item.getValue();
            log.debug("updateDeviceStatus:device:==");
            log.debug(device);
            deviceService.changeStatus(device.get("deviceNo").toString(),device.get("status").toString());
            log.debug(String.format("更改设备的状态,设备(deviceNo:%s)状态:(value:%s)",
                    device.get("deviceNo").toString(),
                    device.get("status").toString()));
            systemService.insertLog(this.getClass().getName(),
                    Thread.currentThread().getStackTrace()[1].getMethodName(),
                    "修改设备状态",String.format("更改设备的状态,设备(deviceNo:%s)状态:(value:%s)",
                            device.get("deviceNo").toString(),
                            device.get("status").toString()),SystemService.LOG_NORMAL);
        }
    }
}
